<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use think\Validate;
use app\index\validate\Valid;
class Common extends Controller
{
    public function _initialize()
    {
        //判断登录
        
    	$login_id=Session::get("login_id");
    	$employee_id=Session::get("employee_id");
    	$type=Session::get("type");
    	if(!($login_id&&$employee_id&&$type))
    		return $this->redirect("Login/signin");
    	//查询登录用户个人信息
    	$login_man = Db::table('employee')->where('id',$employee_id)->find();
    	$this->assign("login_man",$login_man);
    	//查询登录用户的部门列表
    	//查询登录用户的岗位列表
    	//查询登录用户的角色列表
    	//查询登录用户的权限列表
    }
    //我权限下的分区
    public function my_own_fenqu(){
        $uid=Session::get("login_id");
        $eid=Session::get("employee_id");
        $zongarr=db("cg_market_zoning")->field("id")->select();
        $arr=[];
        foreach ($zongarr as $key => $value) {
            # code...
            $arr[]=$value["id"];
        }
        return $arr;
    }
    //我权限下的系统
    public function my_own_sheng(){
        $uid=Session::get("login_id");
        $eid=Session::get("employee_id");
        $zongarr=db("cg_market_provice")->field("id")->select();
        $arr=[];
        foreach ($zongarr as $key => $value) {
            # code...
            $arr[]=$value["id"];
        }
        return $arr;
    }
    //我权限下的城市
    public function my_own_shi(){
        $uid=Session::get("login_id");
        $eid=Session::get("employee_id");
        $zongarr=db("cg_market_city")->field("id")->select();
        $arr=[];
        foreach ($zongarr as $key => $value) {
            # code...
            $arr[]=$value["id"];
        }
        return $arr;
    }
    //我权限下的经销商，经销商业务员+用户单独分配经销商
    public function my_own_jxs(){
        $uid=Session::get("login_id");
        $eid=Session::get("employee_id");
        //经销商业务员
        $jjxs=db("cg_jxs")->where("stuff like '%\"{$eid}\"%'")->field("id")->select();
        $yewuarr=[];
        if($jjxs){
            foreach ($jjxs as $key => $value) {
                $yewuarr[]=$value["id"];
            }
        }
        //用户单独分配
        $ujxs=db("users")->where(["id"=>$uid])->value("jxs_ids");
        $userarr=[];
        if($ujxs){
            $userarr=explode(',', $ujxs);
        }
        //合并查询到的经销商
        $zongarr=array_merge($userarr,$yewuarr);
        //去重复
        $zongarr=array_unique($zongarr);
        //负责人待定
        return $zongarr;
    }
    //我权限下的系统，系统业务员+用户单独分配系统+我的经销商下的系统
    public function my_own_sys(){
        $uid=Session::get("login_id");
        $eid=Session::get("employee_id");
        //系统业务员
        $syss=db("cg_system")->where("stuff like '%\"{$eid}\"%'")->field("id")->select();
        $yewuarr=[];
        if($syss){
            foreach ($syss as $key => $value) {
                $yewuarr[]=$value["id"];
            }
        }
        //用户单独分配
        $usys=db("users")->where(["id"=>$uid])->value("sys_ids");
        $userarr=[];
        if($usys){
            $userarr=explode(',', $usys);
        }
        //合并去重复
        $zongarr=array_merge($yewuarr,$userarr);
        $zongarr=array_unique($zongarr);
        //查询我的经销商
        $jxss=$this->my_own_jxs();
        if($jxss){
            $jxsstr=implode(',',$jxss);
            $jxssarr=db("cg_system")->where("jxs in ({$jxsstr})")->field("id")->select();
            $jxsxiaji=[];
            for($i=0;$i<count($jxssarr);$i++){
                $jxsxiaji[]=$jxssarr[$i]["id"];
            }
            $zongarr=array_merge($zongarr,$jxsxiaji);
            $zongarr=array_unique($zongarr);
        }
        //print_r($zongarr);exit;
        return $zongarr;
    }
    //我权限下的网点  网点业务员+用户单独分配的网点+我经销商/系统下的网点
    public function my_own_net(){
        $uid=Session::get("login_id");
        $eid=Session::get("employee_id");
        //网点业务员
        $syss=db("cg_net")->where("stuff like '%\"{$eid}\"%'")->field("id")->select();
        $yewuarr=[];
        if($syss){
            foreach ($syss as $key => $value) {
                $yewuarr[]=$value["id"];
            }
        }
        //用户单独分配
        $usys=db("users")->where(["id"=>$uid])->value("net_ids");
        $userarr=[];
        if($usys){
            $userarr=explode(',', $usys);
        }
        //合并去重复
        $zongarr=array_merge($yewuarr,$userarr);
        $zongarr=array_unique($zongarr);
        //print_r($zongarr);exit;
        //查询我的系统
        $system=$this->my_own_sys();
        if($system){
            $systemstr=implode(',',$system);
            $sysarr=db("cg_net")->where("system_id in ($systemstr)")->field("id")->select();
            $sysxiaji=[];
            for($i=0;$i<count($sysarr);$i++){
                $sysxiaji[]=$sysarr[$i]["id"];
            }
            $zongarr=array_merge($zongarr,$sysxiaji);
            $zongarr=array_unique($zongarr);
        }
        //print_r($zongarr);exit;
        return $zongarr;
    }
    //下载附件
    public function download_fujian($fid){
        $file=Db::table("annex")->where(["id"=>$fid])->find();
        $path=ROOT_PATH . 'public/uploads/'.$file["path"];
        header("Content-type:text/html;charset=utf-8"); 
         
           
            if(!file_exists($path)){ 
            echo "没有该文件文件"; 
            return ; 
            } 
            $fp=fopen($path,"r"); 
            $file_size=filesize($path); 
            //下载文件需要用到的头 
          /*  Header("Content-type: application/octet-stream"); 
            Header("Accept-Ranges: bytes"); 
            Header("Accept-Length:".$file_size); 
            Header("Content-Disposition: attachment; filename=".$file["file_name"]); 

            $buffer=1024; 
            $file_count=0; 
            //向浏览器返回数据 
            while(!feof($fp) && $file_count<$file_size){ 
            $file_con=fread($fp,$buffer); 
            $file_count+=$buffer; 
            echo $file_con; 
            } 
            fclose($fp); */

             //返回的文件(流形式)
            header("Content-type: application/octet-stream");
            //按照字节大小返回
            header("Accept-Ranges: bytes");
            //返回文件大小
            header("Accept-Length: $file_size");
            //这里客户端的弹出对话框，对应的文件名
            header("Content-Disposition: attachment; filename=".$file["file_name"]);
            //================重点====================
            ob_clean();
            flush();
            //=================重点===================
            //设置分流
            $buffer=1024;
            //来个文件字节计数器
            $count=0;
            while(!feof($fp)&&($file_size-$count>0)){
                $data=fread($fp,$buffer);
                $count+=$data;//计数
                echo $data;//传数据给浏览器端
            }
             fclose($fp);
    }
    /**
     * @param $url
     * @param $post_data
     * @return mixed
     */
    public function curl_post($url,$post_data){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,            $url );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($ch, CURLOPT_POST,           1 );
        curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($post_data));// 必须为字符串
        curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: text/plain'));// 必须声明请求头
        $return = curl_exec($ch);
        curl_close($ch);
        return $return = json_decode($return);
    }

    public function curl_get($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,            $url );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $return = curl_exec($ch);
        curl_close($ch);
        return $return;
    }
    public function fenqutosheng($fid){
        $arr=db("cg_market_provice")->where("isdel=0 and zoning=$fid")->select();
        return json_encode($arr);
    }
    public function shengtoshi($pid){
        $arr=db("cg_market_city")->where("isdel=0 and provice=$pid")->select();
        return json_encode($arr);
    }
    public function shitojxs($cid){
        $arr=db("cg_jxs")->where("isdel=0 and city=$cid")->select();
        return json_encode($arr);
    }
    public function jxstosys($jid){
        $arr=db("cg_system")->where("isdel=0 and jxs=$jid")->select();
        return json_encode($arr);
    }
    public function systonet($sid){
        $arr=db("cg_net")->where("isdel=0 and system_id=$sid")->select();
        return json_encode($arr);
    }
    public function niantokao1($cid){
        $arr=db("cg_kaoheqi1")->where("cid=$cid")->select();
        return json_encode($arr);
    }
    public function kao1tokao2($k1id){
        $arr=db("cg_kaoheqi2")->where("pid=$k1id")->select();
        return json_encode($arr);
    }

    ////////////////////////////////////////////////////////////
    /// 数据验证
    /// ///////////////////////////////////////////////////////
    public function Valid($valid,$data){
        $validate = new Valid($valid);
        if (!$validate->check($data)) {
            echo  json_encode([
                "success"=>false,
                "msg"=>$validate->getError()
            ]);die;
        }
    }
}
